<!-- 车辆管理 -->
<template>
  <div class="dashboard-container driver customer-list-box">
    <div class="app-container">
      <el-card
        class="search-card-box"
        shadow="never"
      >
        <el-form
          ref="form"
          :model="searchFormData"
          label-width="80px"
        >
          <div class="filter-container">
            <el-row :gutter="60">
              <el-col :span="8">
                <el-form-item
                  label="司机账号:"
                  style="margin-bottom: 20px"
                >
                  <el-input
                    v-model="searchFormData.account"
                    placeholder="请输入司机账号"
                    clearable
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="司机姓名:"
                  style="margin-bottom: 20px"
                >
                  <el-input
                    v-model="searchFormData.name"
                    placeholder="请输入司机姓名"
                    clearable
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="司机手机号:"
                  label-width="90px"
                  style="margin-bottom: 20px"
                >
                  <el-input
                    v-model="searchFormData.phone"
                    placeholder="请输入司机手机号"
                    clearable
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="所属机构:"
                  style="margin-bottom: 0px"
                >
                  <treeselect
                    v-model="searchFormData.agentId"
                    :options="agencyOptions"
                    :normalizer="normalizer"
                    :searchable="true"
                    style="width: 100%"
                    placeholder="请选择所属机构"
                    no-options-text="暂无数据"
                    no-results-text="暂无数据"
                    @select="selectAgency"
                    @open="open"
                    @close="close"
                  />
                  <img
                    ref="arrow"
                    src="../../assets/pullDown.png"
                    style="position:absolute;width:18px;height:18px;right:9px;top:12px;cursor:pointer;pointer-events:none;transition-duration:0.3s"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-button
                  type="warning"
                  @click="search"
                >搜索</el-button>
                <el-button
                  class="reset-btn"
                  plain
                  @click="reset"
                >重置</el-button>
              </el-col>
            </el-row>
          </div>
        </el-form>
      </el-card>
      <div
        v-loading="listLoading"
        :class="{ 'loading-box': listLoading }"
        element-loading-text="加载中"
        style="margin-top: 20px"
      >
        <el-card
          class="table-card-box"
          shadow="never"
        >
          <el-table
            :data="driverList"
            fit
            style="width: 100%"
            stripe
            :header-cell-style="{ background: 'rgba(250,252,255,1)' }"
          >
            <div
              v-show="
                (!driverList || driverList.length <= 0) &&
                  !listLoading &&
                  !searchkey
              "
              slot="empty"
            >
              <img
                src="@/icons/pic-kong.png"
                alt="img"
                style="margin-top: 20px; width: 25%; height: 25%"
              />
              <p style="margin-top: -20px; padding-bottom: 0px">这里空空如也</p>
            </div>
            <el-card
              v-show="
                (!driverList || driverList.length <= 0) && !listLoading && searchkey
              "
              slot="empty"
              class="table-empty-box"
              shadow="never"
            >
              <empty></empty>
            </el-card>
            <el-table-column
              align="left"
              label="序号"
              type="index"
              width="100"
            >
              <template v-slot="{ $index }">
                <span>{{ $index + (searchFormData.page - 1) * searchFormData.pageSize + 1 }}</span>
              </template>
            </el-table-column>
            <el-table-column
              align="left"
              label="司机账号"
              min-width="200"
              prop="account"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="司机姓名"
              min-width="200"
              prop="name"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="司机电话"
              min-width="200"
              prop="mobile"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="关联车辆"
              min-width="200"
              prop="truck.licensePlate"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="所属机构"
              min-width="200"
              prop="agency.name"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="工作状态"
              min-width="200"
              prop="workStatus"
            >
              <template v-slot="{ row }">
                <div v-if="row.workStatus === null"></div>
                <div
                  v-else
                  class="tableColumn-status"
                  :class="{ 'stop-use': !row.workStatus }"
                >
                  {{ row.workStatus ? '上班' : '休息' }}
                </div>
              </template>
            </el-table-column>
            <el-table-column
              label="操作"
              align="center"
              fixed="right"
              min-width="130"
              class-name="small-padding fixed-width"
            >
              <template v-slot="{row}">
                <el-link
                  type="primary"
                  :underline="false"
                  @click="view(row.userId)"
                >查看</el-link>
                <el-divider direction="vertical"></el-divider>
                <el-link
                  type="primary"
                  :underline="false"
                  @click="configureVehicle(row)"
                >配置车辆</el-link>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <div class="pagination">
            <div class="pages">
              <el-pagination
                :current-page="searchFormData.page"
                :total="+total"
                :page-size="searchFormData.pageSize"
                :page-sizes="[10, 20, 30, 50]"
                layout="total, sizes, prev, pager, next, jumper"
                @size-change="sizeChange"
                @current-change="currentChange"
              />
            </div>
          </div>
        </el-card>
      </div>
    </div>
    <CarConfigDialog
      ref="arrangeVehiclesDialog"
      class="add-form-dialog"
      :title-info="titleInfo"
      :driver-info="driverInfo"
      :vehicles-info="vehiclesInfo"
      @newDataes="getList"
    ></CarConfigDialog>
  </div>
</template>
<script>
import { agencyList } from '@/api/institutions'
import { driverList, getTruckListInDriver } from '@/api/transit'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import CarConfigDialog from './components/car-config.vue'
export default {
  components: {
    Treeselect,
    CarConfigDialog
  },
  data() {
    return {
      normalizer(node) {
        return {
          id: node.id,
          label: node.name,
          children: node.children
        }
      },
      listLoading: false,
      searchFormData: {
        page: 1,
        pageSize: 10,
        account: '',
        name: '',
        phone: '',
        agentId: null
      },
      agencyOptions: [],
      total: 0,
      driverList: [],
      searchkey: false,
      titleInfo: {
        pageTitle: '',
        text: ''
      },
      driverInfo: {},
      vehiclesInfo: []
    }
  },
  created() {
    this.getList()
    this.agencyList()
    this.getTruckListInDriver()
    const _this = this

    // 键盘enter操作
    document.onkeydown = function(e) {
      const key = window.event.keyCode
      if (key === 13) {
        _this.search()
      }
    }
  },
  methods: {
    async getList() {
      this.listLoading = true
      const { data } = await driverList(this.searchFormData)
      this.driverList = data.items
      this.total = data.counts
      this.listLoading = false
    },
    async agencyList() {
      const res = await agencyList()
      this.agencyOptions = JSON.parse(res.data)
    },
    async getTruckListInDriver() {
      const { data } = await getTruckListInDriver()
      this.vehiclesInfo = data
    },
    selectAgency(val) {
      // 赋予搜索id为搜索框内的数据
      this.searchFormData.agentId = val.id
    },
    // 打开所属机构下拉框
    open() {
      // 将向下三角形，旋转为向右三角形
      this.$refs.arrow.style.transform = 'rotate(-180deg)'
    },
    // 关闭所属机构下拉框
    close() {
      // 将向右三角形，旋转为向上三角形
      this.$refs.arrow.style.transform = 'rotate(0deg)'
    },
    view(id) {
      this.$router.push({
        path: '/transit/driver-detail',
        query: { id, formUrlName: '司机管理' }
      })
    },
    configureVehicle(row) {
      this.$confirm(
        '<p style="margin-bottom: 9px; color: rgb(51, 41, 41);">配置车辆需满足以下条件：</p><p><span style="width: 16px; height: 16px; background: rgb(250, 252, 255); border: 1px solid rgb(235, 238, 245); border-radius: 50%; display: inline-block; text-align: center; margin-right: 6px; line-height: 14px;">1</span><span style="color: rgb(51, 41, 41);">司机信息已完善</span><span style="width: 16px; height: 16px; background: rgb(250, 252, 255); border: 1px solid rgb(235, 238, 245); margin-left: 90px; border-radius: 50%; display: inline-block; text-align: center; line-height: 14px; margin-right: 6px;">2</span><span style="color: rgb(51, 41, 41);">司机已设置排班</span></p><span style="width: 16px; height: 16px; background: rgb(250, 252, 255); border: 1px solid rgb(235, 238, 245); margin-top: 10px; border-radius: 50%; display: inline-block; text-align: center; line-height: 14px; margin-right: 6px;">3</span><span style="color: rgb(51, 41, 41);">司机绑定车辆未在启用状态</span>',
        '配置车辆',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          customClass: 'startconfirm',
          dangerouslyUseHTMLString: true
        }
      )
        .then(() => {
          this.titleInfo.text = '配置车辆'
          Object.assign(this.driverInfo, row)
          this.$nextTick(() => {
            this.$refs.arrangeVehiclesDialog.dialogFormV()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    sizeChange(val) {
      this.searchFormData.pageSize = val
      this.getList()
    },
    currentChange(val) {
      this.searchFormData.page = val
      this.getList()
    },
    search() {
      this.searchFormData.page = 1
      this.searchkey = true
      this.getList()
    },
    reset() {
      this.searchFormData.account = ''
      this.searchFormData.name = ''
      this.searchFormData.phone = ''
      this.searchFormData.agentId = null
      this.searchkey = false
      this.getList()
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 40px;
}
// 下拉框
::v-deep .el-select {
  width: 100%;
}

::v-deep .el-card {
  overflow: initial;
}

.driver {
  ::v-deep .add-form-dialog {
    .el-dialog__body {
      padding: 30px 20px;
    }
    .el-dialog__footer {
      padding: 10px 20px;
      padding-bottom: 20px;
    }
    .el-input__inner {
      color: #606266;
    }
  }
}
</style>

